package mysqlredis;

import java.sql.*;

public class MySqlDemo {
    public static void main(String[] args) {
        String mySqlDriver = "com.mysql.cj.jdbc.Driver";
        String mySqlUrl = "jdbc:mysql://localhost:3306/redisdemo";
        String mySqlUser = "root";
        String mySqlPassword = "";

        Connection conn = null;
        PreparedStatement ps = null;

        try {
            Class.forName(mySqlDriver);
            conn=DriverManager.getConnection(mySqlUrl, mySqlUser, mySqlPassword);
            ps = conn.prepareStatement("select * from student");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                System.out.print(rs.getInt("id")+",");
                System.out.print(rs.getString("name")+",");
                System.out.print(rs.getInt("age")+",");
                System.out.print(rs.getFloat("score")+"\n");
            }

        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try{
                if(ps!=null && !ps.isClosed()){
                    ps.close();
                }
                if(conn!=null && !conn.isClosed()){
                    conn.close();
                }
            }
            catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}
